package com.rtx.wms.std.handler.po.down;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rtx.common.HttpServletRequestUtil;
import com.rtx.model.dto.edi.EdiInnerRequestDTO;
import com.rtx.wms.std.common.bean.WmsConstant;
import com.rtx.wms.std.ctclass.ClassHolder;
import com.rtx.wms.std.po.bean.ApiPo;
import com.rtx.wms.std.po.bean.ApiPodetail;
import com.rtx.wms.std.po.bean.Po;
import com.rtx.wms.std.po.service.ApiPoService;
import com.rtx.wms.std.po.service.PoService;
import com.rtx.wms.std.validation.po.PoCheck;

@SuppressWarnings("all")
public abstract class AbsPoDownHandle {
	
	Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	PoCheck poCheck;
	
	@Autowired
	ApiPoService apiPoservice;
	
	@Autowired
	PoService poService;
 
	public void createPo(HttpServletRequest request) {
    	ApiPo apipo = null;
    	
      	String requestStr = HttpServletRequestUtil.getHttpBody(request);
		EdiInnerRequestDTO dto = JSON.parseObject(requestStr, EdiInnerRequestDTO.class);
		request.setAttribute("config", dto.getConfig());
    	
		//目标json转换成po对象
    	apipo = targetJsonConvertApipo(dto.getTagetjson(),dto);
    	
    	//常规校验
    	poCheck.checkEnterpriseConfig(WmsConstant.ENTERPRISE,apipo);
    	
    	//校验箱明细
    	checkRtxCarton(apipo,dto);
    	
    	//查询单据
    	List<Po> poList = poService.queryByExternpokey(apipo.getWhseid(), apipo.getExternpokey());
    	
    	//处理中间表
    	LambdaQueryWrapper<ApiPo> apipoQw = Wrappers.lambdaQuery();
    	apipoQw.eq(ApiPo::getExternpokey, apipo.getExternpokey());
    	apipoQw.eq(ApiPo::getEdiflag, WmsConstant.DEFAULT_FLAG);
    	List<ApiPo> apipolist = apiPoservice.querApipo("ris_edi", apipoQw);
    	if(apipolist.size() > 0) {
    		throw new RuntimeException("单据锁定处理中！");
    	}
    	
    	
    	if(CollectionUtils.isEmpty(poList)) {
    		//不存在
    		handelNotExistsPo(apipo,dto);
    	}else {
    		//存在
    		handelExistsPo(apipo,poList.get(0),dto);
    	}
	}
	
	//校验箱明细
	protected abstract void checkRtxCarton(ApiPo apipo,EdiInnerRequestDTO dto);
	
	//存在
	protected abstract void handelExistsPo(ApiPo apipo,Po po,EdiInnerRequestDTO dto);
	
	//不存在
	protected abstract void handelNotExistsPo(ApiPo apipo,EdiInnerRequestDTO dto);
	
	
    /**
     * 目标json转换成po对象
     * @throws InvocationTargetException 
     * @throws Exception 
     * 
     */
    public ApiPo targetJsonConvertApipo(String targetJson,EdiInnerRequestDTO dto){
    	ApiPo head = null;
    	try {
    		String apiid =  dto.getConfig().getTargetapiid();
        	Map<String,Class> map = ClassHolder.apiconfigClassMap.get(apiid);
    		if(map!= null && map.get("main")!=null) {
    			Class headclass = map.get("main");
            	Class subclass =map.get("sub");
            	head = (ApiPo) JSON.parseObject(targetJson,headclass);
            	List<ApiPodetail> sub = JSON.parseArray(JSON.parseObject(targetJson).getString("podetail") ,subclass);
            	head.setPodetail(sub);
    		}else {
    			head = JSON.parseObject(dto.getTagetjson(), ApiPo.class);
    		}

		} catch (Exception e) {
			logger.error("目标json转换成po对象异常-",e);
		}

    	return head;
    }
	
    /**
     * 在insert 之后
     * @param request
     * @return
     */
    protected abstract void preinsert(ApiPo r,HttpServletRequest request);
	
}
